﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using BDPAutodetect.Device;
using BDPAutodetect.Hardware;
using BDPAutodetect.Mgrcore;
using BDPAutodetect.Models;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 主页视图模型类
    /// </summary>
    public partial class MainPageViewModel : BaseViewModel<UserControl>
    {
        /// <summary>
        /// 主窗体视图模型实例
        /// </summary>
        protected MainWindowModel MainModel { get; set; }
        /// <summary>
        /// 检测任务管理辅助实例
        /// </summary>
        protected DetectJobManager DetectJobMgr { get; set; }

        /// <summary>
        /// 1号工位检测任务
        /// </summary>
        [ObservableProperty]
        protected DetectJobStation? detectStation1;
        /// <summary>
        /// 2号工位检测任务
        /// </summary>
        [ObservableProperty]
        protected DetectJobStation? detectStation2;
        /// <summary>
        /// 3号工位检测任务
        /// </summary>
        [ObservableProperty]
        protected DetectJobStation? detectStation3;

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public MainPageViewModel(DetectJobManager argDetectJobManager, MainWindowModel argMainWindowModel) : base()
        {
            this.DetectJobMgr = argDetectJobManager;
            this.MainModel = argMainWindowModel;           
        }

        /// <summary>
        /// 页面初始化加载事件
        /// </summary>
        [RelayCommand]
        public async Task ExecuteLoaded()
        {
            if (this.IsLoaded) return;           
            //初始化工位检测信息
            if (!this.DetectJobMgr.IsValidDetect) return;
            foreach (var stationDetectItem in this.DetectJobMgr.StationDetects)
            {
                switch (stationDetectItem.StationType)
                {
                    case  DeviceStationType.Station1:
                        this.DetectStation1 = stationDetectItem;
                        break;
                    case DeviceStationType.Station2:
                        this.DetectStation2 = stationDetectItem;
                        break;
                    case DeviceStationType.Station3:
                        this.DetectStation3 = stationDetectItem;
                        break;
                }
            }
            this.IsLoaded = true;
            await Task.CompletedTask;
        }

        /// <summary>
        /// 1号工位按钮点击命令事件
        /// </summary>
        /// <param name="argCommandType">按钮指令类型</param>
        [RelayCommand]
        public async Task ExecuteButtonStation1(ButtonCommandType argCommandType)
        {
            if (this.DetectStation1 == null) return;
            await this.ExecuteStationDetect(this.DetectStation1, argCommandType);
        }

        /// <summary>
        /// 2号工位按钮点击命令事件
        /// </summary>
        /// <param name="argCommandType">按钮指令类型</param>
        [RelayCommand]
        public async Task ExecuteButtonStation2(ButtonCommandType argCommandType)
        {
            if (this.DetectStation2 == null) return;
            await this.ExecuteStationDetect(this.DetectStation2, argCommandType);
        }

        /// <summary>
        /// 3号工位按钮点击命令事件
        /// </summary>
        /// <param name="argCommandType">按钮指令类型</param>
        [RelayCommand]
        public async Task ExecuteButtonStation3(ButtonCommandType argCommandType)
        {
            if (this.DetectStation3 == null) return;
            await this.ExecuteStationDetect(this.DetectStation3, argCommandType);
        }

        /// <summary>
        /// 工位按钮点击命令事件
        /// </summary>
        /// <param name="argDetectStation"></param>
        /// <param name="argCommandType"></param>
        /// <returns></returns>
        public async Task ExecuteStationDetect(DetectJobStation argDetectStation, ButtonCommandType argCommandType)
        {
            switch (argCommandType)
            {
                case ButtonCommandType.Start:
                    await argDetectStation.StartDetect();
                    break;
                case ButtonCommandType.Stop:
                    await argDetectStation.StopDetect();
                    break;
                case ButtonCommandType.Pause:
                    await argDetectStation.PauseDetect();
                    break;
                case ButtonCommandType.Continue:
                    await argDetectStation.ContinueDetect();
                    break;
                case ButtonCommandType.QueryStationData:
                    await this.MainModel.MenuChanged<QueryRecordDto>("DetectRecord", new QueryRecordDto(argDetectStation.StationType));
                    break;
                case ButtonCommandType.CloseHummerLight:
                    await argDetectStation.SetHummerLightOff();
                    break;
                case ButtonCommandType.SelecTargetParam:
                    this.SelecStationParameter(argDetectStation);
                    break;
                case ButtonCommandType.CleanTime:
                    await argDetectStation.CleanTime();
                    break;
                case ButtonCommandType.CleanNumber:
                    await argDetectStation.CleanNumber();
                    break;
                case ButtonCommandType.SetBarcode:
                    argDetectStation.SetBarcodeManual();
                    break;
            }
        }

        /// <summary>
        /// 工位参数选择
        /// </summary>
        /// <param name="argDetectStation"></param>
        protected void SelecStationParameter(DetectJobStation argDetectStation)
        {
            var parameter = argDetectStation.DetectData.ParameterData;
            ParameterSelectDialog parameterDialog = new ParameterSelectDialog(parameter);
            bool? bResult = parameterDialog.ShowDialog();
            if (!bResult.HasValue || !bResult.Value) return;
            this.DetectJobMgr.SetParameter(argDetectStation.StationType, parameterDialog.DialogResultData);
        }
    }
}
